package com.juyu.miliao.scheduler.service.impl;

import com.juyu.miliao.common.constant.BeansType;
import com.juyu.miliao.common.domain.BillDetailBeans;
import com.juyu.miliao.scheduler.dao.mysql.BillDetailBeansDao;
import com.juyu.miliao.common.domain.BeansGameDTO;
import com.juyu.miliao.scheduler.service.BeansGameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 *  三国游戏入账单和暑期活动的后续操作
 * @Auther: 卜加超
 * @Date: 2019/8/6 15:12
 * @Description:
 */
@Service("beansGameSanGuoServiceImpl")
@Slf4j
public class BeansGameSanGuoServiceImpl implements BeansGameService {
    @Resource
    private BillDetailBeansDao billDetailBeansDao;

    private BillDetailBeans createBillBeansGame(BeansGameDTO beansGameDTO, int beansType, long beans) {
        BillDetailBeans   billDetailBeans = new BillDetailBeans();
        billDetailBeans.setUserId(beansGameDTO.getUserId());
        billDetailBeans.setBeans(beans);
        billDetailBeans.setBeansType(beansType);
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        billDetailBeans.setCreateTime(LocalDateTime.parse(beansGameDTO.getCreateTime(),timeFormatter));
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        billDetailBeans.setCreateDate(LocalDate.parse(beansGameDTO.getCreateDate(),dateFormatter));
        return billDetailBeans;
    }

    @Override
    @Transactional
    public void insertBillBeansGame(BeansGameDTO beansGameDTO) {
        if (!StringUtils.isEmpty(beansGameDTO.getConsume())) {
            if(beansGameDTO.getGameType()==1){
                BillDetailBeans billDetailBeans = createBillBeansGame(beansGameDTO,
                        BeansType.SAN_GUO_CONSUME_BEANS.getCode(), beansGameDTO.getConsume());
                billDetailBeansDao.gameInsert(billDetailBeans);
            }else if(beansGameDTO.getGameType()==2){
                BillDetailBeans billDetailBeans = createBillBeansGame(beansGameDTO,
                        BeansType.CORPSE_GAME_CONSUME__GIVE_BEANS.getCode(), beansGameDTO.getConsume());
                billDetailBeansDao.gameInsert(billDetailBeans);
            }

        }
        if (!StringUtils.isEmpty(beansGameDTO.getProfit())&& beansGameDTO.getProfit()>0) {
            if(beansGameDTO.getGameType()==1){
                BillDetailBeans billDetailBeans = createBillBeansGame(beansGameDTO,
                        BeansType.SAN_GUO_PROFIT_BEANS.getCode(), beansGameDTO.getProfit());
                billDetailBeansDao.gameInsert(billDetailBeans);
            }else if(beansGameDTO.getGameType()==2){
                BillDetailBeans billDetailBeans = createBillBeansGame(beansGameDTO,
                        BeansType.CORPSE_GAME_PROFIT__GIVE_BEANS.getCode(), beansGameDTO.getProfit());
                billDetailBeansDao.gameInsert(billDetailBeans);
            }


        }
    }
}
